package com.sdp.core.bizc.db;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.springframework.context.ApplicationContextException;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

public class QueryUtil {

	public static Object typeConvert(String fieldType,String fieldValue) {
		switch (fieldType) {
		case "class java.lang.Integer":
			return Integer.parseInt(fieldValue);
			
		case "class java.math.BigDecimal":
			return new BigDecimal(fieldValue);
			
		case "class java.lang.Short":
			return  Short.parseShort(fieldValue);
		
		case "class java.lang.Long":
			return  Long.parseLong(fieldValue);
			
		case "class java.lang.Float":
			return   Float.parseFloat(fieldValue);
			
		case "class java.lang.Double":
			return  Double.parseDouble(fieldValue);
			
		case "class java.util.Date":
			return getDateQueryByRule(fieldValue);
			
		case "class java.time.LocalDate":
			return getDateQueryByRule(fieldValue);
			
		case "class java.time.LocalDateTime":
			return getDateQueryByRule(fieldValue);	
		default:
			return fieldValue;
		}
	}
	

	public static Object getDateQueryByRule(String fieldValue) {
			
		if(fieldValue.length()==4) {
			fieldValue = fieldValue+"-01-01 00:00:00";
		}else if(fieldValue.length()==7) {
			fieldValue = fieldValue+"-01 00:00:00";
		}else if(fieldValue.length()==10) {
			fieldValue = fieldValue+" 00:00:00";
		}else if(fieldValue.length()!=19){
			throw new ApplicationContextException("日期格式不对");
		}
//		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//		return Timestamp.valueOf(LocalDateTime.parse(fieldValue, formatter));
		return LocalDateTime.parse(fieldValue, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
	}

				
	public  static void addQueryByRule(QueryWrapper<?> queryWrapper,String rule,String fieldName,String fieldType,String fieldValue) {
		if (QueryRuleEnum.GT.getCode().equals(rule)) {
			queryWrapper.gt(fieldName, typeConvert(fieldType, fieldValue));
		} else if (QueryRuleEnum.GE.getCode().equals(rule)) {
			queryWrapper.ge(fieldName, typeConvert(fieldType, fieldValue));
		} else if (QueryRuleEnum.LT.getCode().equals(rule)) {
			queryWrapper.lt(fieldName, typeConvert(fieldType, fieldValue));
		} else if (QueryRuleEnum.LE.getCode().equals(rule)) {
			queryWrapper.le(fieldName, typeConvert(fieldType, fieldValue));
		} else if (QueryRuleEnum.EQ.getCode().equals(rule)) {
			queryWrapper.eq(fieldName, typeConvert(fieldType, fieldValue));
		} else if (QueryRuleEnum.NE.getCode().equals(rule)) {
			queryWrapper.ne(fieldName, typeConvert(fieldType, fieldValue));
		} else if (QueryRuleEnum.IN.getCode().equals(rule)) {
			Object fv = typeConvert(fieldType, fieldValue);
			if(fv instanceof String) {
				queryWrapper.in(fieldName, (Object[])fv.toString().split(","));
			}else if(typeConvert(fieldType, fieldValue) instanceof String[]) {
				queryWrapper.in(fieldName, (Object[]) fv);
			}else {
				queryWrapper.in(fieldName, fv);
			}
		} else if (QueryRuleEnum.LIKE.getCode().equals(rule)) {
			queryWrapper.like(fieldName, typeConvert(fieldType, fieldValue));
		} else if (QueryRuleEnum.LEFT_LIKE.getCode().equals(rule)) {
			queryWrapper.likeLeft(fieldName, typeConvert(fieldType, fieldValue));
		} else if (QueryRuleEnum.RIGHT_LIKE.getCode().equals(rule)) {
			queryWrapper.likeRight(fieldName, typeConvert(fieldType, fieldValue));
		} else if(QueryRuleEnum.IS_NULL.getCode().equals(rule)){
			queryWrapper.isNull(fieldName);
		}else if(QueryRuleEnum.IS_NOT_NULL.getCode().equals(rule)){
			queryWrapper.isNotNull(fieldName);
		} else {
			throw new ApplicationContextException("查询条件设置错误");
		}
	}
}
